<%@ page import="ro.gateway.aida.utils.CSVParser,
                 ro.gateway.aida.obj.db.AIDALocationDB,
                 ro.gateway.aida.db.PersistenceToken,
                 ro.gateway.aida.db.PersistenceManager,
                 ro.gateway.aida.obj.AIDALocation,
                 java.io.*,
                 ro.gateway.aida.obj.CRegion,
                 ro.gateway.aida.obj.Country"%>
<%
      PersistenceToken token = PersistenceManager.tokenLookup( application );
      AIDALocationDB pman = AIDALocationDB.getManager( token );

      BufferedReader reader = new BufferedReader(
            new FileReader( new File( application.getRealPath("/qqq/continente.tari.csv")))
      );
      String line = reader.readLine();
      try
      {
            while ( line != null )
            {
                  String tokens[] = CSVParser.decode( line );
                  if ( tokens == null || tokens.length != 2 )
                  {
                        line = reader.readLine();
                        continue;
                  }

                  String tara = tokens[ 0 ].trim();
                  String region = tokens[ 1 ].trim();

                  AIDALocation tari[] = pman.findCountries( tara, 10 );

                  CRegion regions[] = pman.findCRegion( region );
                  CRegion found_region = regions != null ? regions[ 0 ] : null;
                  if ( found_region == null )
                  {
                        found_region = new CRegion();
                        found_region.setName( region );
                        pman.insertCRegion( found_region );
                  }


                  if ( tari == null )
                  {
                        out.println( "<font color=\"red\"> Not found:" + tara + "</font><br>" );
                  }
                  else
                  {
                        if ( tari.length == 1 )
                        {
                              out.println( "Found: " + tara + " CRegion:" + region + "<br>" );
                              pman.update_region_for_country(
                                    tari[ 0 ].getCountry().getIso3(),
                                    found_region.getId()
                              );
                              out.println( " Updating country-region link<br>" );
                        }
                        else
                        {
                              Country c = null;
                              for ( int i = 0; i < tari.length; i++ )
                              {
                                    if ( tara.equalsIgnoreCase( tari[ i ].getCountry().getName() ) )
                                    {
                                          c = tari[ i ].getCountry();
                                          break;
                                    }
                              }
                              if ( c != null )
                              {
                                    pman.update_region_for_country(
                                          tari[ 0 ].getCountry().getIso3(),
                                          found_region.getId()
                                    );
                                    out.println( " Updating country-region link<br>" );
                              }
                              else
                              {
                                    out.println( "<font color=\"red\">  Found multiple countries... " +
                                          tara + "hmmm<br>" );
                                    out.println( "  Nothing to do<br></font>" );
                              }
                        }
                  }

                  line = reader.readLine();
                  System.out.println( "line:" + line );
                  out.flush();
            }
      }
      catch ( Exception ex )
      {
            ex.printStackTrace();
      }

%>